{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt# 导入matplotlib\n",
    "from matplotlib import rcParams\n",
    "import pandas as pd\n",
    "\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(0, 10, 100)# 创建x轴的数据\n",
    "y1 = np.sin(x)# 创建y轴的数据\n",
    "y2 = np.cos(x)# 创建y轴的数据\n",
    "\n",
    "plt.figure(figsize=(10, 6))# 创建画布，并指定画布大小 10*6英寸\n",
    "plt.subplot(2, 1, 1)# 创建2行1列个子图，并指定第1个子图\n",
    "plt.xlim(0, 10)# 设置x轴的范围\n",
    "plt.ylim(-1, 1)# 设置y轴的范围\n",
    "plt.xlabel(\"x\")# 设置x轴的标签\n",
    "plt.ylabel(\"sin(x)\")# 设置y轴的标签\n",
    "plt.title(\"sin\")# 设置子图的标题\n",
    "plt.plot(x, y1)# 绘制曲线\n",
    "\n",
    "plt.subplot(2, 1, 2)# 创建2行1列个子图，并指定第2个子图\n",
    "plt.xlim(0, 10)# 设置x轴的范围\n",
    "plt.ylim(-1, 1)# 设置y轴的范围\n",
    "plt.xlabel(\"x\")# 设置x轴的标签\n",
    "plt.ylabel(\"cos(x)\")# 设置y轴的标签\n",
    "plt.title(\"cos\")# 设置子图的标题\n",
    "plt.plot(x, y2)\n",
    "\n",
    "plt.show()# 显示图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "rcParams[\"font.sans-serif\"] = [\"SimHei\"]# 指定中文字体\n",
    "rcParams[\"axes.unicode_minus\"] = False# 解决负号显示问题\n",
    "\n",
    "df = pd.read_csv(\"data/weather.csv\")\n",
    "df.info()# 查看数据集信息\n",
    "\n",
    "\n",
    "fig = plt.figure()\n",
    "ax1 = fig.add_subplot(1, 1, 1)\n",
    "ax1.hist(df[\"precipitation\"], bins=5)#绘制直方图，将降水量均匀分为5组\n",
    "ax1.set_xlabel(\"降水量\")\n",
    "ax1.set_ylabel(\"出现频次\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#双变量\n",
    "rcParams[\"font.sans-serif\"] = [\"SimHei\"]# 指定中文字体\n",
    "rcParams[\"axes.unicode_minus\"] = False# 解决负号显示问题\n",
    "\n",
    "df = pd.read_csv(\"data/weather.csv\")\n",
    "fig = plt.figure()\n",
    "ax1 = fig.add_subplot(1, 1, 1)\n",
    "ax1.scatter(df[\"temp_max\"], df[\"precipitation\"])# 绘制散点图，横轴为最高气温，纵轴为降水量\n",
    "ax1.set_xlabel(\"最高气温\")\n",
    "ax1.set_ylabel(\"降水量\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#双变量\n",
    "rcParams[\"font.sans-serif\"] = [\"SimHei\"]# 指定中文字体\n",
    "rcParams[\"axes.unicode_minus\"] = False# 解决负号显示问题\n",
    "\n",
    "\n",
    "def year_color(x):\n",
    "    \"\"\"添加一列，为不同年份的数据添加不同的颜色\"\"\"\n",
    "    match x.year:\n",
    "        case 2012:\n",
    "            return \"r\"\n",
    "        case 2013:\n",
    "            return \"g\"\n",
    "        case 2014:\n",
    "            return \"b\"\n",
    "        case 2015:\n",
    "            return \"k\"\n",
    "\n",
    "df = pd.read_csv(\"data/weather.csv\")\n",
    "df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "df[\"color\"] = df[\"date\"].apply(year_color)\n",
    "fig = plt.figure()\n",
    "ax1 = fig.add_subplot(1, 1, 1)\n",
    "# 绘制散点图，横轴为最高气温，纵轴为降水量\n",
    "# c设置颜色,alpha设置透明度\n",
    "ax1.scatter(df[\"temp_max\"], df[\"precipitation\"], c=df[\"color\"], alpha=0.5)\n",
    "ax1.set_xlabel(\"最高气温\")\n",
    "ax1.set_ylabel(\"降水量\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#pandas 可视化,单变量\n",
    "df = pd.read_csv(\"data/sleep.csv\")\n",
    "df.info()# 查看数据集信息\n",
    "\n",
    "pd.cut(df[\"sleep_duration\"], [0, 5, 6, 7, 8, 9, 10, 11, 12]).value_counts().plot.bar(color=[\"red\", \"green\", \"blue\", \"yellow\", \"cyan\", \"magenta\", \"black\", \"purple\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df.plot.scatter(x=\"sleep_duration\", y=\"sleep_quality\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot.hexbin(x=\"sleep_duration\", y=\"sleep_quality\", gridsize=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df[\"sleep_quality_stage\"] = pd.cut(df[\"sleep_quality\"], range(11))\n",
    "df[\"sleep_duration_stage\"] = pd.cut(df[\"sleep_duration\"], [0, 5, 6, 7, 8, 9, 10, 11, 12])\n",
    "df_pivot_table = df.pivot_table(values=\"person_id\", index=\"sleep_quality_stage\", columns=\"sleep_duration_stage\", aggfunc=\"count\")\n",
    "df_pivot_table.plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams[\"font.sans-serif\"] = [\"KaiTi\"]\n",
    "penguins = pd.read_csv(\"data/penguins.csv\")\n",
    "penguins.dropna(inplace=True)\n",
    "penguins.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sns.histplot(data=penguins, x=\"species\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sns.kdeplot(data=penguins, x=\"bill_length_mm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sns.histplot(data=penguins, x=\"bill_length_mm\", kde=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.countplot(data=penguins, x=\"island\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sns.scatterplot(data=penguins, x=\"body_mass_g\", y=\"flipper_length_mm\", hue=\"sex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=penguins, x=\"body_mass_g\", y=\"flipper_length_mm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.jointplot(data=penguins, x=\"body_mass_g\", y=\"flipper_length_mm\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
